﻿using System;
using System.Collections.Generic;
using System.Linq;
using Lua;
using UnityEngine;

namespace Assets.Scripts.ProceduralCityGenerator
{
	// Token: 0x02000343 RID: 835
	public class CityHelper
	{
		// Token: 0x060015B1 RID: 5553 RVA: 0x0008D5DA File Offset: 0x0008B7DA
		public static CityBuilding FindRandomStructure(Entity agent)
		{
			return CityHelper.GetBuilding();
		}

		// Token: 0x060015B2 RID: 5554 RVA: 0x0008D5E4 File Offset: 0x0008B7E4
		private static CityBuilding GetBuilding()
		{
			IList<CityBuilding> list = (from x in UnityEngine.Object.FindObjectsOfType<CityBuilding>()
			where !x.broken
			select x).ToList<CityBuilding>();
			if (!list.Any<CityBuilding>())
			{
				return null;
			}
			int index = UnityEngine.Random.Range(0, list.Count - 1);
			return list[index];
		}

		// Token: 0x060015B3 RID: 5555 RVA: 0x0008D644 File Offset: 0x0008B844
		private static bool CheckRadiusForBuildings(UnityEngine.Vector3 position, float radius, ref IList<CityBuilding> buildings)
		{
			bool result = false;
			Collider[] array = Physics.OverlapSphere(position, radius, Layers.buildingLayer);
			foreach (Collider collider in array)
			{
				CityBuilding item;
				if (CityHelper.TryFindBuilding(collider.gameObject, out item))
				{
					buildings.Add(item);
					result = true;
				}
			}
			return result;
		}

		// Token: 0x060015B4 RID: 5556 RVA: 0x0008D6A0 File Offset: 0x0008B8A0
		private static bool TryFindBuilding(GameObject go, out CityBuilding building)
		{
			building = null;
			UnityEngine.Transform transform = go.transform;
			while (transform != null)
			{
				CityBuilding component = transform.gameObject.GetComponent<CityBuilding>();
				if (component != null)
				{
					building = component;
					return true;
				}
				transform = transform.parent;
			}
			return false;
		}
	}
}
